package com.own.business.level.controller;

import com.own.business.level.business.service.LevelConfigService;
import com.own.business.level.entity.dto.LevelConfigDto;
import com.own.business.level.entity.map.LevelConfigMapVo;
import com.own.business.level.entity.query.LevelConfigQuery;
import com.own.business.level.entity.vo.LevelConfigVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.confirm.core.aop.ConfirmAop;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * LevelConfigController
 *
 * @author chenxueli
 * @date 2022-07-25 15:10:00
 */
@Api(tags = "等级配置" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping("level/config")
public class LevelConfigController extends BaseController {

    @Resource
    private LevelConfigService levelConfigService;

    @ApiOperation(value = "分页查询信息")
    @GetMapping("/page")
    public ResultModel<PageModel<LevelConfigVo>> page(
            @ModelAttribute LevelConfigQuery query
    ) {
        var pageModel = levelConfigService.page(query);
        return R.success(pageModel);
    }

    @ApiOperation(value = "map查询信息")
    @GetMapping("/map")
    public ResultModel<List<LevelConfigMapVo>> map(
            @ModelAttribute LevelConfigQuery query
    ) {
        var list = levelConfigService.map(query);
        return R.success(list);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/")
    public ResultModel<LevelConfigVo> add(
            @Validated @RequestBody LevelConfigDto dto
    ) {
        dto.check();
        var updateModel = levelConfigService.add(dto.toBo());
        return R.success(updateModel);
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/{id}")
    public ResultModel<LevelConfigVo> update(
            @ApiParam(value = "主键id", required = true) @PathVariable Long id,
            @Validated @RequestBody LevelConfigDto dto
    ) {
        dto.check();
        var updateModel = levelConfigService.update(id, dto.toBo());
        return R.success(updateModel);
    }

    @ConfirmAop(message = "确认删除该配置？（注意：删除之后无法找回数据）")
    @ApiOperation(value = "删除信息")
    @DeleteMapping("/{id}")
    public ResultModel<Boolean> delete(
            @ApiParam(value = "主键id", required = true) @PathVariable Long id
    ) {
        var updateModel = levelConfigService.delete(id);
        return R.success(updateModel);
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/{id}")
    public ResultModel<LevelConfigVo> getById(
            @ApiParam(value = "主键id", required = true) @PathVariable Long id
    ) {
        var vo = levelConfigService.getById(id);
        return R.success(vo);
    }

}
